package com.example.icanlive;

import android.util.Log;
import android.view.Surface;

public class ICanPlayer {

    private final long nativeHandler;
    private OnErrorListener onErrorListener;
    private OnPrepareListener onPrepareListener;

    public void setOnErrorListener(OnErrorListener onErrorListener) {
        this.onErrorListener = onErrorListener;
    }

    public void setOnPrepareListener(OnPrepareListener onPrepareListener) {
        this.onPrepareListener = onPrepareListener;
    }

    public void setOnProgressListener(OnProgressListener onProgressListener) {
        this.onProgressListener = onProgressListener;
    }

    private OnProgressListener onProgressListener;

    public ICanPlayer() {
        nativeHandler = nativeInit();
    }

    public void setDataSource(String path) {
        setDataSource(nativeHandler, path);
    }

    public void prepare() {
        prepare(nativeHandler);
    }

    public void start() {
        start(nativeHandler);
    }

    public void pause() {

    }

    public void stop() {
        stop(nativeHandler);
    }

    private void onError(int code) {
        if (null != onErrorListener) onErrorListener.onError(code);
    }

    private void onPrepare(int width,int height) {
//        Log.e("ICanPlayer.java","onPrepare 1");
        if (null != onPrepareListener) onPrepareListener.onPrepare(width,height);
    }

    private void onProgress(int progress) {
        if (null != onProgressListener) onProgressListener.onProgress(progress);
    }


    public interface OnErrorListener {
        void onError(int code);
    }

    public interface OnPrepareListener {
        void onPrepare(int width,int height);
    }

    public interface OnProgressListener {
        void onProgress(int progress);
    }

    public void setSurface(Surface surface) {
        setSurface(nativeHandler, surface);
    }

    private native long nativeInit();

    private native void setDataSource(long nativeHandler, String path);

    private native void prepare(long nativeHandler);

    private native void start(long nativeHandler);

    private native void setSurface(long nativeHandler, Surface surface);
    private native void stop(long nativeHandler);
}
